package com.zhh.front.controller;

import com.zhh.api.model.ProductInfo;
import com.zhh.api.pojo.BidInfoProduct;
import com.zhh.api.pojo.MultiProduct;
import com.zhh.common.enums.RCode;
import com.zhh.common.util.CommonUtil;
import com.zhh.front.view.PageInfo;
import com.zhh.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "理财产品功能")
@RestController
@RequestMapping("/v1")
public class ProductController extends BaseController{

    //首页三类产品列表
    @ApiOperation(value = "首页三类产品列表",notes = "一个新手包，三个优选，三个散标产品")
    @GetMapping("/product/index")
    public RespResult queryProductIndex(){

        //把三部分数据取出来
        MultiProduct multiProduct = productService.queryIndexPageProduct();

        //成功，返回对象给前端
        RespResult result = RespResult.ok();
        result.setData(multiProduct);

        return result;
    }

    //分页查询出某个产品的所有记录
    @ApiOperation(value = "产品分页查询",notes = "按照产品类型，分页查询出每种类型的全部产品")
    @GetMapping("/product/list")
    public RespResult queryProductByType(@RequestParam("ptype") Integer pType,
                                         @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                         @RequestParam(value = "pageSize",required = false,defaultValue = "9") Integer pageSize) {
        RespResult result = RespResult.fail();
        if (pType != null && (pType == 0 || pType == 1 || pType == 2)) {
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            //查询某种产品总数
            Integer recordNums = productService.queryRecordNumsByType(pType);
            if (recordNums > 0) {
                //某个产品的所有记录
                List<ProductInfo> productInfos = productService.queryByTypeLimit(pType,pageNo,pageSize);

                result = RespResult.ok();
                result.setList(productInfos);

                //计算总页数
                int totalPage = 0;
                if( recordNums % pageSize  == 0 ){
                    totalPage = recordNums / pageSize;
                } else {
                    totalPage = recordNums / pageSize + 1;
                }

                //某个产品的页号，每页数量，总数量，总页数
                PageInfo pageInfo = new PageInfo(pageNo,pageSize,recordNums,totalPage);
                result.setPage(pageInfo);
            }
        }else{
            result.setRCode(RCode.REQUEST_PRODUCT_TYPE_ERR);
        }
        return result;
    }

    //查询某个产品的详情和投资记录
    @ApiOperation(value = "产品详情",notes = "查询某个产品的详情和投资记录")
    @GetMapping("/product/info")
    public RespResult queryProductDetail(@RequestParam("productId") Integer id){
        RespResult result = RespResult.fail();
        if(id != null && id > 0){
            //查询产品详情
            ProductInfo productInfo = productService.queryById(id);
            if(productInfo != null){ //查询产品成功
                //查询投资记录
                List<BidInfoProduct> bidInfoLists = investService.queryBidListByProductId(id,1,5);
                //成功
                result = RespResult.ok();
                result.setData(productInfo);
                result.setList(bidInfoLists);
            }else{
                result.setRCode(RCode.PRODUCT_OFFLINE);
            }
        }

        return result;
    }
}
